package com.tanchengjin.admin.modules.blog.controller.api.v1.article;

import com.github.pagehelper.PageHelper;
import com.tanchengjin.admin.modules.blog.service.ArticleService;
import com.tanchengjin.admin.modules.blog.utils.ServerResponse;
import com.tanchengjin.admin.modules.cms.mapper.ArticleMapper;
import com.tanchengjin.admin.modules.cms.pojo.Article;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.HashMap;
import java.util.List;
@Api
@RestController
@RequestMapping(value = "${app.api.prefix}/articles")
public class ApiArticleController {
    @Autowired
    private ArticleService articleService;

    @Autowired
    private ArticleMapper articleMapper;

    @ApiOperation(value = "文章列表")
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ServerResponse index(@RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "size", defaultValue = "10") Integer limit) {
        PageHelper.startPage(page, limit);
        List<Article> all = articleService.getAll();
        long count = articleMapper.count();
        HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("list", all);
        objectObjectHashMap.put("count", count);
        return ServerResponse.responseWithSuccess(objectObjectHashMap);
    }

    @ApiOperation(value = "文章详情")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public ServerResponse<Article> show(@PathVariable("id") @NotEmpty long id) {
        Article article = articleMapper.selectByPrimaryKey(id);
        return ServerResponse.responseWithSuccess(article);
    }

    @ApiOperation(value = "获取推荐文件")
    @RequestMapping(value = "/recommend")
    public ServerResponse recommend() {
        int limit = 5;
        List<Article> commentArticle = articleMapper.getCommentArticle();
        return ServerResponse.responseWithSuccess(commentArticle);
    }


}
